<template>
  <div class="advert">
    <el-card>
      <div slot="header">
        <el-button @click="$router.push({
          name: 'advert-create'
        })">添加广告</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="adverts"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="id"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告名称"
        >
        </el-table-column>
        <el-table-column
          prop="spaceId"
          label="广告位置"
        >
          <template slot-scope="scope">
            <span>{{scope.row.spaceId | spaceNameFilter(spaces)}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="img"
          label="广告图片"
        >
          <template slot-scope="scope">
            <img :src="scope.row.img" style="height: 80px;">
          </template>
        </el-table-column>
        <el-table-column
          label="时间"
          width="250"
        >
          <template slot-scope="scope">
            <span>开始时间：{{scope.row.startTime | formatDate}}</span>
            <br>
            <span>到期时间：{{scope.row.endTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="上线/下线"
          width="120"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeAdStatus(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="$router.push({
                name: 'advert-edit',
                params: {
                  advertId: scope.row.id
                }
              })"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-wrapper">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pageNum"
          :disabled="loading"
          :page-sizes="[100, 150, 200]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import {
  getAllSpaces,
  getAdList,
  updateStatus
} from '@/services/advert'

export default Vue.extend({
  name: 'AdvertSpaceIndex',
  data () {
    return {
      loading: false,
      adverts: [],
      spaces: [],
      pageSize: 100,
      pageNum: 1,
      total: 0
    }
  },
  created () {
    this.loadAderts()
  },
  filters: {
    spaceNameFilter (id: any, spaces: any) {
      const space = spaces.find((s: any) => s.id === id) as any
      return space ? space.name : ''
    }
  },
  methods: {
    async loadAderts () {
      this.loading = true
      const [spaceRes, adRes] = await Promise.all([getAllSpaces(), getAdList(this.pageSize, this.pageNum)])
      console.log(spaceRes.data)
      this.spaces = spaceRes.data.content
      this.adverts = adRes.data.content
      this.loading = false
      this.total = this.adverts.length
    },
    handleSizeChange (val: number) {
      this.pageSize = val
      this.pageNum = 1
      this.loadAderts()
    },
    handleCurrentChange (val: number) {
      this.pageNum = val
      this.loadAderts()
    },
    async changeAdStatus (row: any) {
      const msg = Number(row.status) === 0 ? '是否修改下线状态?' : '是否修改为上线状态?'
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await updateStatus(row.id, row.status)
        this.$message.success('操作成功')
        this.loadAderts()
      }).catch(() => {
        this.$message.info('已取消操作!')
        if (row.status === 0) {
          row.status = 1
        } else {
          row.status = 0
        }
      })
    }
  }
})
</script>

<style lang="scss" scoped>
.pagination-wrapper {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>
